#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
    char* NEWENV = "NEWENV=123456";

    
    char* const argv[] = {
                        (char*const)"myother",
                        (char*const)"-a",
                        (char*const)"-b",
                        NULL
    };
    char* const env[] ={
                    (char*const)"MYENV=12345",
                    NULL
    };

    printf("程序运行id%d0\n",getpid());
    //execvpe("./myother",argv,env);  //会把环境变量都覆盖掉
    //execvp("./myother",argv);   //不会把环境变量覆盖
    putenv(NEWENV);//在原有的环境变量上添加新的环境变量
    execvp("./myother",argv);   //不会把环境变量覆盖
    printf("程序运行结束\n");







    /*
    char* const argv[] = {
                        (char*const)"ls",
                        (char*const)"-a",
                        (char*const)"-l",
                        NULL};
    printf("程序运行\n");
    execvp("ls",argv);
    printf("程序运行结束\n");
*/



    /*
    char* const argv[] = {
                        (char*const)"ls",
                        (char*const)"-a",
                        (char*const)"-l",
                        NULL

    };
    printf("程序运行\n");
    execv("/usr/bin/ls",argv);
    printf("程序运行结束\n");
*/


    /*
    printf("程序运行\n");
    execlp("ls","ls","-a","-l",NULL);
    printf("程序运行结束\n");
*/
    /* #1 代码验证出了execl进行程序替换并没有创建新的进程，只是把当前进程的代码和数据用新的代码和数据覆盖的形式替换
    pid_t id = fork();
    if(id == 0)
    {
        printf("子进程pid %d\n",getpid());
       int n = execl("./myother","other.c",NULL);
       printf("%d\n",n);
        exit(1);
        printf("子程序结束\n");
    }

    else
    {
        waitpid(-1,NULL,0);
        printf("父程序\n");
    }
    #1 ******************************************************************************/
    return 0;
}

